package com.sky.controller.user;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.ShoppingCart;
import com.sky.result.Result;
import com.sky.service.ShoppingCartService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 购物车控制器
 *
 * @author liudo
 * @version 1.0
 * @project sky-take-out
 * @date 2024/2/23 12:03:36
 */
@RestController
@RequestMapping("/user/shoppingCart")
@RequiredArgsConstructor
@Slf4j
public class ShoppingCardController {

    private final ShoppingCartService shoppingCartService;
    /**
     * 加购
     *
     * @param shoppingCartDTO
     * @return
     */
    @PostMapping("/add")
    public Result<?> add(@RequestBody ShoppingCartDTO shoppingCartDTO) {
        shoppingCartService.add(shoppingCartDTO);
        return Result.success();
    }

    /**
     * 列表
     *
     * @return {@link Result}<{@link List}<{@link ShoppingCart}>>
     */
    @GetMapping("/list")
    public Result<List<ShoppingCart>> list() {
        Long userId = BaseContext.getCurrentId();
        List<ShoppingCart> shoppingCartList = shoppingCartService.listByUserId(userId);
        return Result.success(shoppingCartList);
    }

    /**
     * 清空购物车
     *
     * @return {@link Result}<{@link ?}>
     */
    @DeleteMapping("/clean")
    public Result<?> clean() {
        Long userId = BaseContext.getCurrentId();
        shoppingCartService.cleanByUserId(userId);
        return Result.success();
    }
 }
